#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "ISPPage.h"
#include "ISPDlg.h"

#include "../client/SettingsManager.h"
#include "../client/ClientProfileManager.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

PropPage::TextItem ISPPage::texts[] = {
	{ IDC_ADD_ISP, ResourceManager::ADD },
	{ IDC_CHANGE_ISP, ResourceManager::SETTINGS_CHANGE },
	{ IDC_REMOVE_ISP, ResourceManager::REMOVE },
	{ 0, ResourceManager::SETTINGS_AUTO_AWAY }
};


LRESULT ISPPage::onInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	PropPage::translate((HWND)(*this), texts);

	CRect rc;

	ctrlISP.Attach(GetDlgItem(IDC_ISP));
	ctrlISP.GetClientRect(rc);

	ctrlISP.InsertColumn(0, CTSTRING(IP_RANGE), LVCFMT_LEFT, rc.Width() / 2, 0);
	ctrlISP.InsertColumn(1, CTSTRING(ISP), LVCFMT_LEFT, rc.Width() / 2, 1);

	if(BOOLSETTING(FULL_ROW_SELECT))
		ctrlISP.SetExtendedListViewStyle(LVS_EX_FULLROWSELECT);

	// Do specialized reading here
	Isp::List& il = ClientProfileManager::getInstance()->getIspList(); 
	StringList cols;
	for(Isp::Iter j = il.begin(); j != il.end(); ++j) {
		tstring ran = Util::toIP(j->second->getLower()) + '-' + Util::toIP(j->first);
		cols.push_back(ran);
		cols.push_back(j->second->getIsp());
		ctrlISP.insert(cols);
		cols.clear();
	}
	return TRUE;
}

LRESULT ISPPage::onAddISP(WORD , WORD , HWND , BOOL& ) {
	ISPDlg dlg;

	if(dlg.DoModal() == IDOK) {
		ClientProfileManager::getInstance()->insertISP(Util::getUpperRange(dlg.range), Util::getLowerRange(dlg.range), dlg.ISP, dlg.bad);
		StringList lst;
		lst.push_back(dlg.range);
		lst.push_back(dlg.ISP);
		ctrlISP.insert(lst);
	}
	return 0;
}

LRESULT ISPPage::onChangeISP(WORD , WORD , HWND , BOOL& ) {
	
#define BUFLEN 256
	if(ctrlISP.GetSelectedCount() == 1) {
		int sel = ctrlISP.GetSelectedIndex();
		TCHAR buf[BUFLEN];
		ISPDlg dlg;
		ctrlISP.GetItemText(sel, 1, buf, BUFLEN);
		dlg.ISP = buf;
		ctrlISP.GetItemText(sel, 0, buf, BUFLEN);
		dlg.range = buf;
		u_int32_t high = Util::getUpperRange(buf);
		dlg.bad = ClientProfileManager::getInstance()->isBadRange(high);

		if(dlg.DoModal() == IDOK) {
			ClientProfileManager::getInstance()->removeISP(high);
			ClientProfileManager::getInstance()->insertISP(Util::getUpperRange(dlg.range), Util::getLowerRange(dlg.range), dlg.ISP, dlg.bad);
			ctrlISP.SetItemText(sel, 0, dlg.range.c_str());
			ctrlISP.SetItemText(sel, 1, dlg.ISP.c_str());
		}
	}
	return 0;
}

LRESULT ISPPage::onRemoveISP(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	if(ctrlISP.GetSelectedCount() == 1) {
		TCHAR buf[256];
		ctrlISP.GetItemText(ctrlISP.GetSelectedIndex(), 0, buf, 256);
		ClientProfileManager::getInstance()->removeISP(Util::getUpperRange(buf));
		ctrlISP.DeleteItem(ctrlISP.GetNextItem(-1, LVNI_SELECTED));
	}
	return 0;
}

LRESULT ISPPage::onReload(WORD , WORD , HWND , BOOL& ) {
	reload();
	return 0;
}

void ISPPage::reload() {
	ctrlISP.SetRedraw(FALSE);
	
	ClientProfileManager::getInstance()->reloadISPs();
	ctrlISP.DeleteAllItems();
	
	Isp::List& il = ClientProfileManager::getInstance()->getIspList(); 
	StringList cols;

	for(Isp::Iter j = il.begin(); j != il.end(); ++j) {
		tstring ran = Util::toIP(j->second->getLower()) + '-' + Util::toIP(j->first);
		cols.push_back(ran);
		cols.push_back(j->second->getIsp());
		ctrlISP.insert(cols);
		cols.clear();
	}
	ctrlISP.SetRedraw(TRUE);
}

LRESULT ISPPage::onCustomDraw(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/) {
	LPNMLVCUSTOMDRAW cd = (LPNMLVCUSTOMDRAW)pnmh;

	switch(cd->nmcd.dwDrawStage) {
	case CDDS_PREPAINT:
		return CDRF_NOTIFYITEMDRAW;

	case CDDS_ITEMPREPAINT:
		{
			try {
				TCHAR buf[256];
				ctrlISP.GetItemText(cd->nmcd.dwItemSpec, 0, buf, 256);
				if(ClientProfileManager::getInstance()->isBadRange(Util::getUpperRange(buf))) {
					cd->clrText = SETTING(BAD_CLIENT_COLOUR);
				}
				return CDRF_NEWFONT | CDRF_NOTIFYSUBITEMDRAW;
			} catch(...) {
			}
		}
		return CDRF_NOTIFYSUBITEMDRAW;

	default:
		return CDRF_DODEFAULT;
	}
}

void ISPPage::write() {
	ClientProfileManager::getInstance()->saveISPs();
}
/**
 * @file
 * $Id: ISPPage.cpp 303 2005-08-18 23:46:43Z sulan $
 */